<template>
  <div class="container">
    <el-form :model="formInfo" ref="formInfo">
      <el-form-item label="所属学科" label-width="100px" v-if="!query.id">
        <el-select v-model="formInfo.subjectID" placeholder="请选择">
          <el-option v-for="item in simpleSubject" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" label-width="100px" prop="tagName">
        <el-input v-model="formInfo.tagName" placeholder="请输入标签名称"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="clickOk">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags'
export default {
  name: 'tagsDialog',
  props: {
    //获取状态 修改还是添加
    isState: {
      type: String,
      require: true
    },
    // 获取简易学科列表
    simpleSubject: {
      type: Array,
      require: true
    },
    // 获取点击项的数据
    getVal: {
      type: Object,
      require: true
    },
    // 获取查询参数里
    query: {
      type: [Object],
      require: true
    }
  },
  data() {
    return {
      formInfo: {
        id: null, //数据id
        subjectID: this.isState == 'edit' ? this.getVal.subjectID : null, //学科id
        tagName: this.isState == 'edit' ? this.getVal.tagName : '' //标签名称
      }
    }
  },
  methods: {
    async clickOk() {
      if (this.isState == 'add') {
        if (this.query.id) {
          this.formInfo.subjectID = parseInt(this.query.id)
        }
        try {
          await add(this.formInfo)
          this.$message.success('添加成功')
          this.$emit('close')
        } catch (error) {
          console.log(error)
          this.$message.error('添加失败')
        }
      } else {
        this.formInfo.id = this.getVal.id
        try {
          await update(this.formInfo)
          this.$message.success('修改成功')
          this.$emit('close')
        } catch (error) {
          console.log(error)
          this.$message.error('修改失败')
        }
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
